package com.sgb.goods.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.BuildConfig;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.BrandManagerEntity;
import com.sgb.goods.entity.ProListEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.utils.module.GoodsModule;
import com.sgb.goods.view.adapter.BrandManagerAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * 品牌管理
 * @author cyj
 */
public class BrandManagerModel extends BaseViewModel {

    private int page = 1;
    public BrandManagerAdapter adapter;
    public MutableLiveData<List<BrandManagerEntity>> brandList = new MutableLiveData<>();

    public BrandManagerModel() {
        adapter = new BrandManagerAdapter();
    }

    /**
     * 查询品牌列表
     * @param isLoadMore    true：加载更多；false：下拉刷新
     */
    public void getBrandList(boolean isLoadMore) {
        showLoading();
        page = isLoadMore ? page + 1 : Constants.PAGE_NUM;
        String companyId = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.REAL_ID, "");
        GoodsNet.getInstance().getGoodsApi().getBrandList(page, Constants.PAGE_SIZE_20, companyId).enqueue(new GoodsCallBack<BaseEntity<ProListEntity<List<BrandManagerEntity>>>>() {

            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<BrandManagerEntity>>> data) {
                brandList.setValue(data.getData().getList());
                if(isLoadMore) {
                    adapter.addData(data.getData().getList());
                } else {
                    adapter.setNewData(data.getData().getList());
                }
            }

            @Override
            public void onFail() {
                brandList.setValue(null);
            }
        });
    }

    /** 删除品牌 */
    public void delete(String id, int position) {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().deleteBrand(id).enqueue(new GoodsCallBack<BaseEntity<Object>>() {

            @Override
            public void onSuccess(BaseEntity<Object> data) {
                adapter.getData().remove(position);
                adapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 获取品牌操作权限
     * @return
     */
    public boolean getBrandOperationPermission() {
        if(BuildConfig.IS_MODULE_DEBUG) {
            return true;
        } else {
            Object object = GoodsModule.getInstance().call2("getBrandOperationPermission");
            return null != object && (Boolean) object;
        }
    }
}
